package com.persondemo.videoappliction.ui.vip;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.bean.UseInfoBean;
import com.persondemo.videoappliction.component.ApplicationComponent;
import com.persondemo.videoappliction.component.DaggerHttpComponent;
import com.persondemo.videoappliction.event.MainEvent;
import com.persondemo.videoappliction.runtimepermissions.PermissionsManager;
import com.persondemo.videoappliction.runtimepermissions.PermissionsResultAction;
import com.persondemo.videoappliction.ui.base.BaseFragment;
import com.persondemo.videoappliction.ui.history.HistoryActivity;
import com.persondemo.videoappliction.ui.vip.contract.MeContract;
import com.persondemo.videoappliction.ui.vip.presenter.MePresenter;
import com.persondemo.videoappliction.utils.ImageLoaderUtil;
import com.persondemo.videoappliction.utils.PreferenceUtil;
import com.persondemo.videoappliction.utils.RxDeviceTool;
import com.persondemo.videoappliction.utils.T;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2018/4/19.
 * 个人中心
 */

public class MeFragment extends BaseFragment<MePresenter> implements MeContract.View {
    @BindView(R.id.me_head_img)
    CircleImageView meHeadImg;
    @BindView(R.id.me_setting)
    RelativeLayout meSetting;
    @BindView(R.id.me_tiezi)
    TextView meTiezi;
    @BindView(R.id.me_teizi_liner)
    LinearLayout meTeiziLiner;
    @BindView(R.id.me_dianzan)
    TextView meDianzan;
    @BindView(R.id.me_dianzan_liner)
    LinearLayout meDianzanLiner;
    @BindView(R.id.me_message)
    TextView meMessage;
    @BindView(R.id.me_message_liner)
    LinearLayout meMessageLiner;
    @BindView(R.id.me_liner_01)
    RelativeLayout meLiner01;
    @BindView(R.id.me_liner_02)
    RelativeLayout meLiner02;
    @BindView(R.id.me_liner_03)
    RelativeLayout meLiner03;
    @BindView(R.id.me_vip_liner)
    RelativeLayout meVipLiner;
    @BindView(R.id.me_name)
    TextView meName;
    Unbinder unbinder;
    @BindView(R.id.me_sex)
    ImageView meSex;
    @BindView(R.id.me_comment_liner)
    RelativeLayout meCommentLiner;
    @BindView(R.id.me_shop_liner)
    RelativeLayout meShopLiner;
    @BindView(R.id.reate_setting)
    RelativeLayout reateSetting;
    private String avatar = "";//头像
    private String username = "";
    String imei = "";
    private String[] huancunArry = new String[]{"100M", "1G", "3G"};

    public static MeFragment newInstance() {
        Bundle args = new Bundle();
        MeFragment fragment = new MeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.module_frament_me;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {
        DaggerHttpComponent.builder()
                .applicationComponent(appComponent)
                .build()
                .inject(this);
    }


    @Override
    public void bindView(View view, Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
    }

    private void check() {
        PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(getActivity(), new String[]{
                Manifest.permission.READ_PHONE_STATE
        }, new PermissionsResultAction() {
            @Override
            public void onGranted() {
                imei = RxDeviceTool.getIMEI(getActivity());
                if (!TextUtils.isEmpty(imei)) {
                    showLoadingDialog();
                    PreferenceUtil.put("imei", imei);
                    if (mPresenter != null) {
                        mPresenter.info(imei);
                    }
                }
            }

            @Override
            public void onDenied(String permission) {
                T.showShort(getActivity(), "用户拒绝授权");
            }
        });
    }

    @Override
    public void initData() {
        check();

    }

    @OnClick({R.id.me_setting, R.id.me_teizi_liner, R.id.me_dianzan_liner,
            R.id.me_message_liner, R.id.me_liner_01, R.id.me_comment_liner,
            R.id.me_liner_02, R.id.me_liner_03, R.id.me_vip_liner, R.id.me_shop_liner,R.id.reate_setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.me_setting:
                startActivity(new Intent(getActivity(), PersonSettingActivity.class));
                getActivity().overridePendingTransition(R.anim.slide_in_left, R.anim.slide_in_right);
                break;
            //我的帖子
            case R.id.me_teizi_liner:
//                T.showShort(getContext(), "我的帖子");
                break;
            //点赞
            case R.id.me_dianzan_liner:
//                T.showShort(getContext(), "点赞");
                break;
            //消息回复
            case R.id.me_message_liner:
//                T.showShort(getContext(), "回复");
                break;
            //审帖
            case R.id.me_liner_01:
                if (TextUtils.isEmpty(imei)) {
                    T.showShort(getActivity(), "未获取到imei权限，无法使用此功能！请从设置打开授予权限！");
                    return;
                } else {
                    startActivity(new Intent(getActivity(), ShenTieActivity.class));
                    getActivity().overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
                }
                break;
            //收藏
            case R.id.me_liner_02:
                if (TextUtils.isEmpty(imei)) {
                    T.showShort(getActivity(), "未获取到imei权限，无法使用此功能！请从设置打开授予权限！");
                    return;
                } else {
                    startActivity(new Intent(getActivity(), CollectActivity.class));
                    getActivity().overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
                }
                break;
            //最近浏览
            case R.id.me_liner_03:
                startActivity(new Intent(getActivity(), HistoryActivity.class));
                getActivity().overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
                break;
            //会员中心
            case R.id.me_vip_liner:
                startActivity(new Intent(getActivity(), VipActivity.class));
                getActivity().overridePendingTransition(R.anim.slide_in_left, R.anim.slide_in_right);
                break;
            //缓存设置
            case R.id.me_comment_liner:
                showhuancun();
                break;
            case R.id.me_shop_liner:
                startActivity(new Intent(getActivity(), MeShopActivity.class));
                getActivity().overridePendingTransition(R.anim.slide_in_left, R.anim.slide_in_right);
                break;
            case R.id.reate_setting:
                startActivity(new Intent(getActivity(), PersonSettingActivity.class));
                getActivity().overridePendingTransition(R.anim.slide_in_left, R.anim.slide_in_right);
                break;
        }
    }

    private void showhuancun() {
        int a = 0;
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());// 自定义对话框
        if (PreferenceUtil.getInt("huancun", 0) == 1) {
            a = 0;
        } else if (PreferenceUtil.getInt("huancun", 0) == 2) {
            a = 1;
        } else if (PreferenceUtil.getInt("huancun", 0) == 3) {
            a = 2;
        } else {
            a = 0;
        }
        builder.setSingleChoiceItems(huancunArry, a, new DialogInterface.OnClickListener() {// 2默认的选中
            @Override
            public void onClick(DialogInterface dialog, int which) {// which是被选中的位置

//                personSex.setText(huancunArry[which]);100M", "500M","1G
                if (huancunArry[which].equals("100M")) {
                    PreferenceUtil.put("huancun", 1);
                    T.showShort(getActivity(), "设置成功！");
                } else if (huancunArry[which].equals("1G")) {
                    PreferenceUtil.put("huancun", 2);
                    T.showShort(getActivity(), "设置成功！");
                } else {
                    PreferenceUtil.put("huancun", 3);
                    T.showShort(getActivity(), "设置成功！");
                }
                dialog.dismiss();// 随便点击一个item消失对话框，不用点击确认取消
            }
        });
        builder.show();// 让弹出框显示
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Subscriber
    private void gomovie(MainEvent event) {
        if (event == null) return;
        String title = event.title;
        if (title.equals("更新用户信息")) {
            if (mPresenter != null) {
                mPresenter.info(imei);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }

    @Override
    public void getinfo(UseInfoBean useInfoBean) {
        hideLoadingDialog();
        if (useInfoBean.getStatus() == 1) {
            meTiezi.setText(useInfoBean.getData().getTies() + "");
            meDianzan.setText(useInfoBean.getData().getZansnum() + "");
            meMessage.setText(useInfoBean.getData().getMsgnum() + "");
            ImageLoaderUtil.LoadImagelarge(getActivity(), useInfoBean.getData().getAvatar(), meHeadImg);
            PreferenceUtil.put("email", useInfoBean.getData().getEmail());
            PreferenceUtil.put("username",useInfoBean.getData().getUsername());
            PreferenceUtil.put("avatar",  useInfoBean.getData().getAvatar());
            Log.i("touxiang01", "ulr=" + useInfoBean.getData().getAvatar());
            meName.setText(useInfoBean.getData().getUsername());
            if (useInfoBean.getData().getSex() == 0) {
                meSex.setBackgroundResource(R.drawable.wuman);
            } else {
                meSex.setBackgroundResource(R.drawable.man);
            }
        } else {
            T.showShort(getActivity(), useInfoBean.getMsg());
        }
    }

}
